package com.fscratch;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/*应用启动类*/
//Spring Boot 应用的标识
@SpringBootApplication
public class FromScratchApplication {

    public static void main(String[] args) {
        // 程序启动入口
        // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
        SpringApplication.run(FromScratchApplication.class, args);
    }

    // 注入httpMessageConverters 使用fastjson 它的速度之快超过了所有json库。甚至是以前最快的jackson.
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {

        // 创建转换对象
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        // 创建配置文件对象
        FastJsonConfig config = new FastJsonConfig();
        // SerializerFeature属性参考 https://blog.csdn.net/u010246789/article/details/52539576
        // 处理中问乱码
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        converter.setSupportedMediaTypes(fastMediaTypes);

        config.setSerializerFeatures(SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteDateUseDateFormat// 进行日期格式化 yyyy-MM-dd
        );

        converter.setFastJsonConfig(config);
        return new HttpMessageConverters(converter);

    }

}
